<?php
	require("libraries/student.php");
  	class Album extends Student{
		public function __construct(){
			parent::__construct();
			$this->load->helper("url");
			$this->load->helper("form");
			$this->load->library("form_validation");
			$this->load->model("model_album");
		}
		public function index(){
			$config['base_url'] = base_url().'admin/album/index/';
			$config['total_rows'] = $this->model_album->count_all();
			$config['per_page'] = 10;
			$config['uri_segment'] = 4;
			$config['next_link'] = "Next";
			$config['prev_link'] = "Prev";
			$this->load->library("pagination");
			$this->pagination->initialize($config); 
			$start = $this->uri->segment(4);
			$data['data'] = "";
			$data['title']   = "Manage albums";
			$data['template'] = "albums/list_album";
			$data['act'] = 0;
			$data['listall']= $this->model_album->listall($config['per_page'],$start);
			$this->load->view("layout",$data);
		}
		public function add(){
			$data['data'] = "";
			$data['title']   = "Add new album";
			$data['template'] = "albums/add_album";
			$data['act'] = 0;
			$data['listcate'] = $this->model_album->listcategory();
			if($this->input->post("ok") != NULL){
				$name  = $this->input->post("album_name");
				$order = $this->input->post("album_order");
				$cate  = $this->input->post("cate_id");
				$db = array(
					"album_name" => $name,
					"album_order" => $order,
					"album_status" => "1",
					"cate_id" => $cate
				);
				if($_FILES['image']['name'] != NULL){
					$config['upload_path'] = './uploads/albums';
					$config['allowed_types'] = 'jpg|png|jpeg|gif';
					$config['max_size']	= '2000';
					$config['max_width']  = '1750';
					$config['max_height']  = '1750';
					$this->load->library('upload',$config);
					if(!$this->upload->do_upload("image")){
						$data = array('error' => $this->upload->display_errors());
						$data['data'] = "";
						$data['title']   = "Add new album";
						$data['template'] = "albums/add_album";
						$data['act'] = 0;
						$data['listcate'] = $this->model_album->listcategory();
						$this->load->view("layout",$data);
						return FALSE;
					}else{
						$data = $this->upload->data();
						$db['album_image'] = $data['file_name'];
						$this->createThumbnail($db['album_image'],"albums",100,100);
					}
				}
				$file = count($_FILES['fullimage']['name']);
				$img = array();
				$now = "";
				if($file > 1){
					for($i=0;$i<count($file);$i++){
						$name = $_FILES['fullimage']['name'][$i];
						$type = $_FILES['fullimage']['type'][$i];
						$size = $_FILES['fullimage']['size'][$i];
						if($type == "image/jpeg" || $type == "image/png" || $type == "image/gif"){
							if($size > 1048576){
								die("File size not larger than 1mb");
							}else{
								$now = date("dmYhis");
								if(!file_exists("uploads/albums/".$now)){
									mkdir("uploads/albums/".$now,0700);
								}
								$db['album_forder'] = $now;
								move_uploaded_file($_FILES['fullimage']['tmp_name'][$i],"uploads/albums/".$now."/".$_FILES['fullimage']['name'][$i]);
								$img[] = $_FILES['fullimage']['name'][$i];
							}
						}else{
							die("Invalid file type : ".$type);
						}
						//echo $name." - ".$type." - ".$size."<br />";
					}
					$db['album_fullimage'] = serialize($img);
				}
				$this->model_album->add($db);
				redirect(base_url()."admin/album");
			}else{
				$this->load->view("layout",$data);
			}
		}
		public function update(){
			$id = $this->uri->segment(4);
			$data['data'] = "";
			$data['title']   = "Edit album";
			$data['template'] = "albums/edit_album";
			$data['act'] = 0;
			$data['getdata'] = $this->model_album->getdata($id);
			$data['listcate'] = $this->model_album->listcategory();
			$data['stt'] = $data['getdata']['cate_id'];
			if($this->input->post("ok") != NULL){
				$name  = $this->input->post("album_name");
				$order = $this->input->post("album_order");
				$cate  = $this->input->post("cate_id");
				$db = array(
					"album_name" => $name,
					"album_order" => $order,
					"album_status" => "1",
					"cate_id" => $cate
				);
				if($_FILES['image']['name'] != NULL){
					$config['upload_path'] = './uploads/albums';
					$config['allowed_types'] = 'jpg|png|jpeg|gif';
					$config['max_size']	= '2000';
					$config['max_width']  = '1750';
					$config['max_height']  = '1750';
					$this->load->library('upload',$config);
					if(!$this->upload->do_upload("image")){
						$data = array('error' => $this->upload->display_errors());
						$data['data'] = "";
						$data['title']   = "Edit album";
						$data['template'] = "albums/edit_album";
						$data['act'] = 0;
						$data['getdata'] = $this->model_album->getdata($id);
						$data['listcate'] = $this->model_album->listcategory();
						$data['stt'] = $data['getdata']['cate_id'];
						$this->load->view("layout",$data);
						return FALSE;
					}else{
						$data = $this->upload->data();
						$db['album_image'] = $data['file_name'];
						$this->createThumbnail($db['album_image'],"albums",100,100);
					}
				}
				$file = count($_FILES['fullimage']['name']);
				$img = array();
				$now = "";
				if($file > 1){
					for($i=0;$i<$file;$i++){
						$name = $_FILES['fullimage']['name'][$i];
						$type = $_FILES['fullimage']['type'][$i];
						$size = $_FILES['fullimage']['size'][$i];
						if($type == "image/jpeg" || $type == "image/png" || $type == "image/gif"){
							if($size > (2*1048576)){
								die("File size not larger than 2mb");
							}else{
								$now = date("dmYhis");
								if(!file_exists("uploads/albums/".$now)){
									mkdir("uploads/albums/".$now,0700);
								}
								$db['album_forder'] = $now;
								move_uploaded_file($_FILES['fullimage']['tmp_name'][$i],"uploads/albums/".$now."/".$_FILES['fullimage']['name'][$i]);
								$img[] = $_FILES['fullimage']['name'][$i];
							}
						}else{
							die("Invalid file type : ".$type);
						}
						//echo $name." - ".$type." - ".$size."<br />";
					}
					$db['album_fullimage'] = serialize($img);
				}
				$this->model_album->update($db,$id);
				redirect(base_url()."admin/album");
			}else{
				$this->load->view("layout",$data);
			}
		}
		public function active(){
			$id   	= $this->uri->segment(4);
			$status	= $this->uri->segment(5);
			$db = array(
				"album_status" => $status 
			);
			$this->model_album->update_status($db,$id);
			redirect(base_url()."admin/album/");
		}
		public function delete(){
			$id = $this->uri->segment(4);
			$data = $this->model_album->getdata($id);
			if($data == NULL){ die("This tour is not avaible");}
			$dirPath = "uploads/albums/".$data['album_forder'];
			if(! is_dir($dirPath)) {
		        throw new InvalidArgumentException("$dirPath must be a directory");
		    }
		    if(substr($dirPath, strlen($dirPath) - 1, 1) != '/') {
		        $dirPath .= '/';
		    }
		    $files = glob($dirPath . '*', GLOB_MARK);
		    foreach ($files as $file) {
		        if (is_dir($file)) {
		            rmdir($file);
		        } else {
		            unlink($file);
		        }
		    }
		    rmdir($dirPath);
			$this->model_album->del($id);
			redirect(base_url()."admin/album");
		}
		public function createThumbnail($fileName,$path,$width,$height){
			$this->load->library('image_lib');
			//$this->load->helper('thumbnail_helper');
			$config['image_library'] = 'gd2';
			$config['source_image'] = 'uploads/'.$path.'/'.$fileName;
			$config['new_image']    = 'uploads/'.$path.'/thumb/'.$fileName;
			$config['create_thumb'] = TRUE;
			$config['maintain_ratio'] = TRUE;
			$config['thumb_marker'] = FALSE;
			$config['width'] = $width;
			$config['height'] = $height;
			$this->image_lib->initialize($config); 
			$this->image_lib->resize();
			$this->image_lib->clear();
		}
	}